package com.ca.singlesignon.service.serviceImpl;

import com.ca.singlesignon.entity.UserEntity;
import com.ca.singlesignon.repository.UserRepository;
import com.ca.singlesignon.service.UserService;
import com.ca.singlesignon.utils.MD5Util;
import com.ca.singlesignon.utils.ServerResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<UserEntity> findAll() {
        return userRepository.findAll();
    }

    @Override
    public ServerResponse<UserEntity> login(String username, String password) {
        //使用MD5+盐值加密
        String MD5Password = MD5Util.MD5EncodeUtf8(password);
        // 密码登录MD5
        UserEntity entity = userRepository.findUserEntityByUsernameAndPassword(username, MD5Password);
        if (entity == null) {
            return ServerResponse.createByErrorMessage("密码错误");
        }
        //登录成功后把密码设置成空
        entity.setPassword(StringUtils.EMPTY);
        return ServerResponse.createBySuccess("登录成功", entity);
    }
}
